
fn main() {
    let nums = vec![2, 7, 3, 4];
    let target = 7;
    let res = Solution::two_sum(nums, target);
    println!("{:#?}", res);
}

struct Solution;

impl Solution {
    pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
        let mut result = vec![];
        let mut map = std::collections::HashMap::new();
        for i in 0..nums.len() {
            let num = nums[i];
            let want = target - num;
            if map.contains_key(&want) {
                let index = map.get(&want).unwrap();
                result.push(*index as i32);
                result.push(i as i32);
                break;
            }
            map.insert(num, i);
        }
        result
    }
}
